/**
 * 
 */
package com.tdam.g12.adapters;

import java.util.ArrayList;
import com.tdam.g12.soporte.Contacto;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author G12
 *
 */
public class ContactosAdapter extends BaseAdapter {
	
	private ArrayList<Contacto> contactos;
	private LayoutInflater inflater;
	
	public ContactosAdapter(Context context){
		contactos = new ArrayList<Contacto>();
		inflater = LayoutInflater.from(context);	
	}
	
	public void clear() {
		contactos.clear();
	}

	@Override
	public int getCount() {
		return contactos.size();
	}
	
	public void addItem(Contacto contacto){
		if (contacto != null) 
		{
			contactos.add(contacto);
		}
	}

	@Override
	public Object getItem(int position) {
		return contactos.get(position);
	}
	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		Holder holder = null;
		
		if (convertView == null) 
		{
			convertView = inflater.inflate(com.tdam.g12.R.layout.item_contacto, null);
			holder = new Holder();
			holder.imgAvatar = (ImageView) convertView.findViewById(com.tdam.g12.R.id.imgAvatarItemContacto);
			holder.txtUsuarioWeb = (TextView) convertView.findViewById(com.tdam.g12.R.id.txtNombre);
			convertView.setTag(holder);
		} 
		else 
		{
			holder = (Holder) convertView.getTag();
		}

		Contacto contacto = (Contacto) contactos.get(position);
		
		holder.imgAvatar.setImageResource(contacto.getAvatar());
		holder.txtUsuarioWeb.setText(contacto.getNombre());

		return convertView;
	}
	
	/*
	 * Clase interna Holder
	 */
	class Holder 
	{
		private TextView txtUsuarioWeb;
		private ImageView imgAvatar;
	}
}
